package com.inossem.allotin.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.inossem.allotin.R;
import com.inossem.allotin.adapter.RealWhCodeAdapter;
import com.inossem.allotin.adapter.ReceiptTypeAdapter;
import com.inossem.allotin.adapter.input.ShipOutBinListAdapter;
import com.inossem.allotin.adapter.input.ShipOutTypeListAdapter;
import com.inossem.allotin.bean.TrayReceiptTypeBean;
import com.inossem.allotin.db.CacheBean;
import com.inossem.allotin.view.LinePathView;
import com.inossem.wms.model.cus.CusRealWh;
import com.inossem.wms.model.dic.DicWhStorageBin;
import com.inossem.wms.vo.auth.DicWhStorageTypeTreeVo;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * ----------
 * Author: Leo
 * Date: 2019/5/22
 * Email: 18304097708@163.com
 * Description:
 * ----------
 */

public class DialogUtils {

    /**
     * RFID 选择前序单据
     */
    public static Dialog getReceiptTypeDialog(Activity activity, List<TrayReceiptTypeBean> typeList, String code, ReceiptTypeAdapter.OnChooseReceiptType listener) {
        return BaseDialog.createDialog(activity, R.layout.dialog_type_bin_choose, 0.4f, 0.6f,
                (context, view, dialog) -> {
                    final int[] receiptCodes = new int[1];
                    final String[] receiptNames = new String[1];
                    RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
                    LinearLayoutManager layoutManager = new LinearLayoutManager(context);
                    recyclerView.setLayoutManager(layoutManager);
                    recyclerView.setAdapter(new ReceiptTypeAdapter(activity, typeList, (receiptCode, receiptName) -> {
                        receiptCodes[0] = receiptCode;
                        receiptNames[0] = receiptName;
                        listener.receiptTypeChoose(receiptCodes[0], receiptNames[0]);
                        dialog.dismiss();
                    }));

                });
    }

    /**
     * RFID选择实际仓库
     */
    public static Dialog getRealWhCodeDialog(Activity activity, List<CusRealWh> list, String code, RealWhCodeAdapter.OnItemListener listener) {
        return BaseDialog.createDialog(activity, R.layout.dialog_type_bin_choose, 0.4f, 0.6f,
                (context, view, dialog) -> {
                    RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
                    LinearLayoutManager layoutManager = new LinearLayoutManager(context);
                    recyclerView.setLayoutManager(layoutManager);
                    RealWhCodeAdapter adapter = new RealWhCodeAdapter(activity, code, list, (cellCode, typeName, isCellEnabled) -> {
                        if (!cellCode.equals(code)) {
                            listener.onItemClickListener(cellCode, typeName, isCellEnabled);
                            dialog.dismiss();
                        } else {
                            dialog.cancel();
                        }
                    });
                    recyclerView.setAdapter(adapter);
                });
    }

    public interface OnSignClickListener {
        void onClickListener(String title, String sign);
    }

    /**
     * 签字
     */
    public static Dialog getSignDialog(Activity activity, OnSignClickListener listener) {
        return BaseDialog.createDialog(activity, R.layout.dialog_sign, 0.6f, 0.6f,
                (context, view, dialog) -> {
                    EditText et_title = view.findViewById(R.id.et_title);
                    LinePathView lpv_view = view.findViewById(R.id.lpv_view);
                    TextView tvNo = view.findViewById(R.id.tv_no);
                    tvNo.setOnClickListener(v1 -> dialog.dismiss());
                    TextView tvYes = view.findViewById(R.id.tv_yes);
                    tvYes.setOnClickListener(v1 -> {
                        try {
                            String title = et_title.getText().toString().trim();
//                            if (TextUtils.isEmpty(title)) {
//                                ToastUtils.showToast("请输入抬头备注！");
//                                return;
//                            }
                            if (!lpv_view.getTouched()) {
                                ToastUtils.showToast("您还没有签名！");
                                return;
                            }
                            String signFile = lpv_view.save();
                            listener.onClickListener(title, signFile);
                            dialog.dismiss();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
                });
    }

    /**
     * 查看签字
     */
    public static Dialog checkSignDialog(Activity activity, CacheBean cacheBean) {
        return BaseDialog.createDialog(activity, R.layout.dialog_sign_check, 0.6f, 0.6f,
                (context, view, dialog) -> {
                    TextView tv_title = view.findViewById(R.id.tv_title);
                    ImageView iv_sign = view.findViewById(R.id.iv_sign);
                    tv_title.setText(cacheBean.getRemark());
                    Glide.with(activity).load(cacheBean.getSignName()).into(iv_sign);
                    TextView tvNo = view.findViewById(R.id.tv_no);
                    tvNo.setOnClickListener(v1 -> dialog.dismiss());
                    TextView tvYes = view.findViewById(R.id.tv_yes);
                    tvYes.setOnClickListener(v1 -> {
                        dialog.dismiss();
                    });
                });
    }

    public interface OnRemarkClickListener {
        void onClickListener(String title);
    }

    /**
     * 备注
     */
    public static Dialog getRemarkDialog(Activity activity, String remark, boolean isEdit, OnRemarkClickListener listener) {
        return BaseDialog.createDialog(activity, R.layout.dialog_remark, 0.6f, 0.6f,
                (context, view, dialog) -> {
                    EditText et_title = view.findViewById(R.id.et_title);
                    et_title.setText(remark);
                    if (!isEdit) {
                        et_title.setFocusable(false);
                        et_title.setCursorVisible(false);
                        et_title.setFocusableInTouchMode(false);
                        et_title.setBackground(null);
                    }
                    TextView tvNo = view.findViewById(R.id.tv_no);
                    tvNo.setOnClickListener(v1 -> dialog.dismiss());
                    TextView tvYes = view.findViewById(R.id.tv_yes);
                    tvYes.setOnClickListener(v1 -> {
                        String title = et_title.getText().toString().trim();
                        listener.onClickListener(title);
                        dialog.dismiss();
                    });
                });
    }

    public interface OnClickListener {
        void onClickListener();
    }

    /**
     * 提示
     */
    public static Dialog getMsgDialog(Context activity, OnClickListener listener) {
        return BaseDialog.createDialog(activity, R.layout.dialog_msg, 0.4f, 0.5f,
                (context, view, dialog) -> {
                    TextView tv_msg = view.findViewById(R.id.tv_msg);
                    tv_msg.setText("是否删除！");
                    TextView tvNo = view.findViewById(R.id.tv_no);
                    tvNo.setOnClickListener(v1 -> dialog.dismiss());
                    TextView tvYes = view.findViewById(R.id.tv_yes);
                    tvYes.setOnClickListener(v1 -> {
                        listener.onClickListener();
                        dialog.dismiss();
                    });
                });
    }

    /**
     * ScrollMsgDialog
     */
    public static Dialog getMsgDialogScrollLongName(Context activity, String msg) {
        return BaseDialog.createDialog(activity, R.layout.dialog_msg_scroll_name, (context, view, dialog) -> {
            TextView msgTv = view.findViewById(R.id.tv_msg);
            TextView sureTv = view.findViewById(R.id.tv_sure);
            sureTv.setVisibility(View.GONE);
            msgTv.setText(msg);
            sureTv.setOnClickListener(view1 -> dialog.dismiss());
        });
    }

    public interface OnChooseSaveTypeListener {
        void onChoose(DicWhStorageTypeTreeVo vo);
    }

    public interface OnChooseBinListener {
        void onChoose(DicWhStorageBin vo);
    }

    /**
     * 选择存储类型Dialog
     */
    public static Dialog getChooseSaveTypeDialog(Activity activity, List<DicWhStorageTypeTreeVo> list, String type, OnChooseSaveTypeListener listener) {
        return BaseDialog.createDialog(activity, R.layout.dialog_ship_out_choose_type_bin_view,
                (context, view, dialog) -> {
                    TextView tvTitle = view.findViewById(R.id.tv_title);
                    RecyclerView rv = view.findViewById(R.id.rv);
                    TextView tvCancel = view.findViewById(R.id.tv_cancel);
                    TextView tvSure = view.findViewById(R.id.tv_sure);

                    tvTitle.setText(R.string.allotapply_module_hint_plz_choose_type);

                    rv.setLayoutManager(new LinearLayoutManager(activity));
                    ShipOutTypeListAdapter adapter = new ShipOutTypeListAdapter(activity, list, type);
                    rv.setAdapter(adapter);

                    tvCancel.setOnClickListener(v -> dialog.dismiss());

                    tvSure.setOnClickListener(v -> {
                        if (adapter.getCheckBean() == null) {
                            ToastUtils.showToast(R.string.allotapply_module_hint_plz_choose_type);
                        } else {
                            if (listener != null) {
                                listener.onChoose(adapter.getCheckBean());
                            }
                            dialog.dismiss();
                        }
                    });
                });
    }

    /**
     * 选择目标仓位Dialog
     */
    public static Dialog getChooseBinDialog(Activity activity, List<DicWhStorageBin> list, String type, OnChooseBinListener listener) {
        return BaseDialog.createDialog(activity, R.layout.dialog_ship_out_choose_type_bin_view,
                (context, view, dialog) -> {
                    TextView tvTitle = view.findViewById(R.id.tv_title);
                    RecyclerView rv = view.findViewById(R.id.rv);
                    TextView tvCancel = view.findViewById(R.id.tv_cancel);
                    TextView tvSure = view.findViewById(R.id.tv_sure);

                    tvTitle.setText(R.string.allotapply_module_hint_plz_choose_bin);

                    rv.setLayoutManager(new LinearLayoutManager(activity));
                    ShipOutBinListAdapter adapter = new ShipOutBinListAdapter(activity, list, type);
                    rv.setAdapter(adapter);

                    tvCancel.setOnClickListener(v -> dialog.dismiss());

                    tvSure.setOnClickListener(v -> {
                        if (adapter.getCheckBean() == null) {
                            ToastUtils.showToast(R.string.allotapply_module_hint_plz_choose_bin);
                        } else {
                            if (listener != null) {
                                listener.onChoose(adapter.getCheckBean());
                            }
                            dialog.dismiss();
                        }
                    });
                });
    }

    /**
     * ScrollMsgDialog
     */
    public static Dialog getMsgDialogScroll(Context activity, String msg) {
        return BaseDialog.createDialog(activity, R.layout.dialog_msg_scroll, 0.4f, 0.4f, (context, view, dialog) -> {
            TextView msgTv = view.findViewById(R.id.tv_msg);
            TextView sureTv = view.findViewById(R.id.tv_sure);
            msgTv.setText(msg);
            sureTv.setOnClickListener(view1 -> dialog.dismiss());
        });
    }

    /**
     * MsgDialog 只有确定键
     */
    public static Dialog getShowMsgDialog(Activity activity, String msg, OnClickListener listener) {
        Dialog resultDialog = BaseDialog.createDialog(activity, R.layout.dialog_show_msg, 0.364583f, 0.46296f, (context, view, dialog) -> {
            TextView msgTv = view.findViewById(R.id.tv_msg);
            TextView sureTv = view.findViewById(R.id.tv_sure);

            msgTv.setText(msg);

            sureTv.setOnClickListener(view1 -> {
                listener.onClickListener();
                dialog.dismiss();
            });

        });

        resultDialog.setCancelable(false);
        resultDialog.setCanceledOnTouchOutside(false);

        return resultDialog;
    }
}
